import java.util.Arrays;
import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: mac
 * Date: 2022-10-14
 * Time: 14:33
 */
public class Test {
    //直接插入排序
    public static void insertSort(int[] array){
        for (int i = 0; i < array.length; i++) {
            //傀儡变量保存节点值
            int tmp = array[i];
            int j = i - 1;
            for (;j >= 0; j--){
                if (array[j] > tmp){
                    array[j + 1] = array[j];
                }else{
                    break;//找到合适位置退出循环
                }
            }
            array[j + 1] = tmp;//插入排序
        }
    }
    //折半插入排序
    public static void bsinsertSort(int[] array){
        for (int i = 0; i < array.length; i++) {
            int ret = array[i];
            int left = 0;
            int right = i;
            //由于插入排序数据会逐渐趋于有序，所以可用折半查找节点值
            while (left < right){
                int mid = left + (right - left) / 2;
                if (array[mid] < ret){
                    left = mid + 1;
                }else {
                    right = mid;
                }
            }
            for (int j = i - 1; j >= left; j--) {
                array[j + 1] = array[j];
            }
            array[left] = ret;
        }
    }

    //希尔排序
    public static void shellSort(int[] array){
        int gap = array.length;
        while (gap > 1){
            shell(array, gap);
            gap /= 2;
        }
        shell(array, 1);
    }
    public static void shell(int[] array, int gap){
        for (int i = 0; i < array.length; i++) {
            int ret = array[i];
            int j = i - gap;
            for (; j >= 0; j -= gap) {
                if (array[j] > ret){
                    array[j + gap] = array[j];
                }else{
                    break;
                }
            }
            array[j + gap] = ret;
        }
    }

    //交换
    public static void swap(int[] array, int i, int j){
        int tmp = array[i];
        array[i] = array[j];
        array[j] = tmp;
    }
    //选择排序
    public static void selectSort(int[] array){
        for (int i = 0; i < array.length; i++) {
            int min = i;
            for (int j = i + 1; j < array.length; j++) {
                if (array[j] < array[min]){
                    min = j;
                }
            }
            swap(array, i, min);
        }
    }
    //双向选择排序
    public static void selectSortOP(int[] array){
        int low = 0;
        int high = array.length - 1;
        while (low <= high){
            int min = low;
            int max = high;
            for (int i = low + 1; i <= high; i++) {
                if (array[i] < array[min]){
                    min = i;
                }
                if (array[i] > array[max]){
                    max = i;
                }
            }
            swap(array, low, min);
            if (max == low){
                max = min;
            }
            swap(array, high, max);
            low++;
            high--;
        }
    }
    //堆排序
    public static void heapSort(int[] array){
        createHeap(array);
        for (int end = array.length - 1; end >= 0; end--) {
            swap(array, 0, end);
            shiftDown(array, 0, end);
        }
    }
    public static void createHeap(int[] array){
        for (int parent = (array.length - 1 - 1) / 2; parent >= 0; parent--) {
            shiftDown(array, parent, array.length);
        }
    }
    public static void shiftDown(int[] array, int parent, int len){
        int child = parent * 2 + 1;
        while (child < len){
            if (child + 1 < len && array[child] < array[child + 1]){
                child++;
            }
            if (array[parent] < array[child]){
                swap(array, parent, child);
                parent = child;
                child = child * 2 + 1;
            }else{
                break;
            }
        }
    }
    //冒泡排序
    public static void bubbleSort(int[] array){
        for (int i = 0; i < array.length - 1; i++) {
            boolean flg = false;
            for (int j = 0; j < array.length - i - 1; j++) {
                if (array[j] > array[j + 1]){
                    swap(array, j , j + 1);
                    flg = true;
                }
            }
            if (flg == false){
                break;
            }
        }
    }
    //快速排序
    public static void insertSort2 (int[] array, int start, int end){
        for (int i = 1; i < end; i++) {
            int tmp = array[i];
            int j = i - 1;
            for (; j >= start; j--) {
                if (array[j] > tmp){
                    array[j + 1] = array[j];
                }else{
                    break;
                }
            }
            array[j + 1] = tmp;
        }
    }
    public static void quickSort(int[] array){
        quick(array, 0, array.length - 1);
    }
    public static void quick(int[] array, int left, int right){
        if (left > right){
            return;
        }
        if (right - left + 1 <= 1600){
            insertSort2(array, left, right);
        }
        //优化思路
        int midValIndex = findMidValIndex(array, left, right);
        swap(array, midValIndex, left);

        int privot = partition(array, left, right);
        quick(array, privot + 1, right);
        quick(array, left, privot - 1);
    }
    public static int findMidValIndex(int[] array, int left, int right){
        int mid = left + ((right - left) >>> 1);
        if (array[left] < array[right]){
            if (array[mid] < array[left]){
                return left;
            }else if(array[mid] > array[right]){
                return right;
            }else{
                return mid;
            }
        }else{
            if (array[mid] > array[left]){
                return left;
            }else if (array[mid] < array[right]){
                return right;
            }else{
                return mid;
            }
        }
    }
    public static int partition(int[] array, int left, int right){
        int tmp = array[left];
        while (left < right){
            while (left < right && array[right] >= tmp){//加上等号
                right--;
            }
            array[left] = array[right];
            while (left < right && array[left] <= tmp){
                left++;
            }
            array[right] = array[left];
        }
        array[left] = tmp;
        return left;
    }
    //非递归快排
    public static void quickSort1(int[] array){
        Stack<Integer> stack = new Stack<>();
        int left = 0;
        int right = array.length - 1;
        int privot = partition(array, left, right);
        if (privot > left + 1){
            stack.push(left);
            stack.push(privot - 1);
        }
        if (privot < right - 1){
            stack.push(privot + 1);
            stack.push(right);
        }

        while (!stack.isEmpty()){
            right = stack.pop();
            left = stack.pop();
            privot = partition(array, left, right);
            if (privot > left + 1){
                stack.push(left);
                stack.push(privot - 1);
            }
            if (privot < right - 1){
                stack.push(privot + 1);
                stack.push(right);
            }
        }
    }
    //归并排序
    public static void mergeSort(int[] array){
        mergeSortInternal(array,0,array.length - 1);
    }
    public static void mergeSortInternal(int[] array, int low, int high){
        if (low >= high){
            return;
        }
        int mid = low + ((high - low) >>> 1);
        mergeSortInternal(array, low, mid);
        mergeSortInternal(array, mid + 1, high);
        merge(array, low, mid, high);
    }
    public static void merge(int[] array, int low, int mid, int high){
        int tmp[] = new int[high - low + 1];
        int s1 = low;
        int e1 = mid;
        int s2 = mid + 1;
        int e2 = high;
        int k = 0;
        while (s1 <= e1 && s2 <= e2){
            if (array[s1] < array[s2]){
                tmp[k++] = array[s1++];
            }else{
                tmp[k++] = array[s2++];
            }
        }
        while (s1 <= e1){
            tmp[k++] = array[s1++];
        }
        while (s2 <= e2){
            tmp[k++] = array[s2++];
        }
        for (int i = 0; i < k; i++) {
            array[low + i] = tmp[i];
        }
    }
    //非递归归并排序
    public static void mergeSort1(int[] array){
        int nums = 1;
        while (nums < array.length){
            for (int i = 0; i < array.length; i += nums * 2) {
                int left = i;
                int mid = left + nums - 1;
                if (mid > array.length - 1){
                    mid  = array.length - 1;
                }
                int right = mid + nums;
                if (right > array.length - 1){
                    right = array.length - 1;
                }
                merge(array, left, mid, right);
            }
            nums *= 2;
        }
    }
    //测试函数
    public static void main(String[] args) {
        int[] array = {12,5,9,34,6,0,33,56,89,0,7,4,55,77};
        //insertSort(array);
        //bsinsertSort(array);
        //shellSort(array);
        //selectSort(array);
        //selectSortOP(array);
        //heapSort(array);
        //bubbleSort(array);
        //quickSort(array);
        //quickSort1(array);
        //mergeSort(array);
        mergeSort1(array);
        System.out.println(Arrays.toString(array));
    }
}
